package com.jumi.microservice.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.dto.reponse.UserCashExternalResponse;
import com.jumi.microservice.dto.request.JmUserUpdateRequest;
import com.jumi.microservice.entity.JmJpushUser;
import com.jumi.microservice.entity.JmUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jumi.microservice.entity.vo.JmUserSetVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import javax.swing.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员表 Mapper 接口
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-07-14
 */
public interface JmUserMapper extends BaseMapper<JmUser> {

    Page<JmUser> userPage(Page<JmUser> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select u1.*,u2.`name` upUserMobile from jm_user u1 left join (select id,name,truename,mobile from jm_user) u2 on u1.inviter_id=u2.id where u1.id=#{id}")
    JmUserUpdateRequest getUserInfoById(@Param("id") Integer id);

    @Select("SELECT u.id,u.name,u.truename,u.mobile,u.avatar,u.sex,u.passwd,u.wechat_num\n" +
            ",u.paypwd,u.email,IFNULL(u.inviter_id,0)as pid,IFNULL(p.name,'') as p_name\n" +
            ",IFNULL(p.truename,'') as p_true_name\n" +
            "FROM jm_user u\n" +
            "LEFT JOIN jm_user p ON u.inviter_id=p.id\n" +
            "where u.id=#{memberId} and u.`status`=1")
    JmUserSetVo findUserSetById(@Param("memberId") Integer memberId);


    @Select("SELECT \n" +
            "  u.`level` AS userLevel,\n" +
            "  IFNULL(i.`status`, 0) AS identityStatus,\n" +
            "  IFNULL(r.`status`, 0) AS realStatus,\n" +
            "  r.`idcard` AS idCard,\n" +
            "  r.`truename` AS trueName,\n" +
            "  r.`bank_card` AS bankCard,\n" +
            "  r.`ali_code` AS aliCode,\n" +
            "  IFNULL(g.`status`, 0) AS goldStatus,\n" +
            "  g.`ali_code` AS goldAliCode \n" +
            "FROM\n" +
            "  `jm_user` u \n" +
            "  LEFT JOIN `jm_identity_authentication` i \n" +
            "    ON u.id = i.uid \n" +
            "  LEFT JOIN `jm_realname_authentication` r \n" +
            "    ON u.id = r.uid \n" +
            "  LEFT JOIN `jm_gold_promoter_review` g \n" +
            "    ON u.id = g.`member_id` \n" +
            "WHERE u.id = #{uid} \n" +
            "ORDER BY i.create_time DESC,\n" +
            "  r.create_time DESC,\n" +
            "  g.create_time DESC \n" +
            "LIMIT 1 ")
    UserCashExternalResponse getUserCashByUid(@Param("uid") Integer uid);
}
